package coop.bancocredicoop.proyectos.gd

import org.apache.commons.logging.LogFactory

import coop.bancocredicoop.crecerxxi.server.XStreamSerializer;
import coop.bancocredicoop.crecerxxi.server.exposicion.ResponseTO;
import coop.bancocredicoop.service.core.response.OutputMessage;
import coop.bancocredicoop.service.core.sender.backport.socket.BackportMessageSender;
import coop.bancocredicoop.service.load.ServiceProvider;
import coop.bancocredicoop.service.transaccion.persona.factory.T24MessageSpecFactory;
import coop.bancocredicoop.service.transaccion.persona.parser.spec.ListaCandidatosMessageSpec;
import coop.bancocredicoop.service.transaccion.persona.specParser.ListaCandidatosTextBeanImpl;

import coop.bancocredicoop.proyectos.gd.alfresco.Utils
import coop.bancocredicoop.proyectos.gd.alfresco.CASClient

class HomeController {

    private static final log = LogFactory.getLog(this)
    def mailService

    def index() { 
        session.gd = null
    }

    def gd() {
        session.gd = params.gd
        render(view: 'index')
    }

    def logout() {
        session.invalidate() 
        redirect(action: 'index')
    }

    def testCASAuth() {
        
        def usuario = session.usuario
        def userName = usuario.getUserName()

        def client = new Utils(grailsApplication.config.alfresco)
                            .getClient(loggedInUser: userName)
        def result = client.hello() 
        render(result) 

    }

    def testCrecer21() { 
		ResponseTO response = null;
		
				ListaCandidatosMessageSpec spec = (ListaCandidatosMessageSpec) (new T24MessageSpecFactory()
						.getListasCandidatos());
		
				spec.withTipoPersona("A");
		
				//spec.withNroInscripcion("30701756661");
				spec.withApellido('ASANN')
				
				spec.setParseable(false);
		
				BackportMessageSender service = (BackportMessageSender) ServiceProvider
						.getInstance().lookupService(BackportMessageSender.class);
		
				OutputMessage output = service.send(spec);
		
				response = (ResponseTO) XStreamSerializer.getInstance().deserialize(
						(String) output.getData());
				
				List<ListaCandidatosTextBeanImpl> candidatos = (List<ListaCandidatosTextBeanImpl>) response.dataFromXML();
				
				System.out.println(candidatos.size());
				System.out.println(((ListaCandidatosTextBeanImpl)candidatos.get(0)));
				System.out.println(((ListaCandidatosTextBeanImpl)candidatos.get(1)));
				System.out.println(((ListaCandidatosTextBeanImpl)candidatos.get(1)).getNroInscripcionEnte());

    }
    
    def testSendMail() {
        
        mailService.sendMail {
            to params.to 
            from 'gdcarga@localhost'
            subject params.subject
            body "Mail from GD"
        }
 
        render "EMail sent."
    }

}
